package com.breeze.login.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.breeze.common.login.net.LoginAddress;
import com.breeze.common.login.net.LoginBean;
import com.breeze.login.api.OrderServices;

import java.io.IOException;

import brouter.annotation.BRouter;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Retrofit;

@BRouter(path = "/login/getLoginBean")
public class LoginAddressImpl implements LoginAddress {
    private final static String BASE_URL = "http://apis.juhe.cn/";

    @Override
    public LoginBean getLoginBean(String key, String ip) throws IOException {
        // 网络请求返回结果LoginBean
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .build();
        OrderServices host = retrofit.create(OrderServices.class);

        // Retrofit GET同步请求
        Call<ResponseBody> call = host.get(ip, key);

        retrofit2.Response<ResponseBody> response = call.execute();
        if (response != null && response.body() != null) {
            JSONObject jsonObject = JSON.parseObject(response.body().string());
            LoginBean loginBean = jsonObject.toJavaObject(LoginBean.class);
            System.out.println("login组件中独有的网络请求功能：解析后结果 >>> " + loginBean.toString());
            return loginBean;
        }
        return null;
    }
}
